<template>
  <u-navbar
    title="首页"
    class="home-navbar"
    :autoBack="false"
    leftIcon=""
    bgColor="#FBFBFF"
    :style="{ paddingTop: safeAreaInsets?.top + 'px' }"
  >
    <!-- <template #center>
			<view class="u-nav-slot">
				<scan-bar></scan-bar>
			</view>
		</template> -->
    <!-- <template #right>
			<u-icon name="scan" size="24px" @click="openScan"></u-icon>
		</template> -->
  </u-navbar>
  <view class="content uni-content">
    <view class="menu-area">
      <view
        v-for="(item, index) in menu"
        :key="index"
        class="menuCard"
        @click="jump(item)"
      >
        <image
          :src="'/static/icons/' + item.icon + '.svg'"
          style="width: 70rpx; height: 70rpx; color: aqua"
        ></image>
        <view class="title">{{ item.name }}</view>
        <u-badge
          type="error"
          max="999"
          :value="item.info"
          shape="horn"
          class="info"
        ></u-badge>
      </view>
      <view style="word-wrap: break-word">{{ scanCode }}</view>
    </view>
  </view>
  <!-- <u-button type="primary" text="upload" @click="jump({enabled: 1, url:'demo/upload/index'})"></u-button> -->
</template>

<script lang="ts" setup>
import { onMounted, ref } from "vue";
import { useMenuStore } from "@/stores/useMenuStore";
// import ScanBar from '@/components/scan-bar/index.vue';
import { onShow } from "@dcloudio/uni-app";
import { http } from "@/utils/http";

const { safeAreaInsets } = uni.getSystemInfoSync();
// 获取首页菜单
const menuStore = useMenuStore();
const menu = ref([]);

onShow(() => {
  const orginalMenu = menuStore.menu.home;
  // console.log('menuStore.menu.home', menuStore.menu.home)
  menu.value = orginalMenu.filter((item) => item.hidden === false);
  // 获取模具的未读信息
  getMoldInfo();
  getEamInfo();
});

const infoList = [
  {
    url: "eam/mold/maintainPlan/index",
    name: "maintenanceCount",
    des: "模具预防维护",
  },
  {
    url: "eam/mold/plannedMaintenance/index",
    name: "planCount",
    des: "模具计划维修",
  },
  {
    url: "eam/mold/repairHandle/index",
    name: "faultCount",
    des: "模具故障维修",
  },
  {
    url: "eam/mold/outWarehouse/index",
    name: "outboundCount",
    des: "模具出库",
  },
];

const getMoldInfo = async () => {
  await http({
    method: "POST",
    url: "/eam/mouldAssetInfo/repairCountList",
    data: {},
  }).then((res) => {
    for (let i = 0; i < menu.value.length; i++) {
      for (let j = 0; j < infoList.length; j++) {
        if (infoList[j].url === menu.value[i].url) {
          menu.value[i].info = res.data[infoList[j].name] ?? 0;
          break;
        }
      }
    }
  });
};

const infoList1 = [
  {
    url: "eam/equ/repairOrder/index",
    name: "faultCount",
    des: "故障报修",
  },
  {
    url: "eam/equ/repairHandle/index",
    name: "repairCount",
    des: "故障维修",
  },
  {
    url: "eam/equ/routingInspection/index",
    name: "routeInspectionCount",
    des: "巡检任务",
  },
  
  {
    url: "eam/equ/maintainPlan/index",
    name: "maintainCount",
    des: "预防性",
  },
  {
    url: "eam/equ/selfMaintainPlan/index",
    name: "selfMaintainCount",
    des: "自主保全",
  },
  {
    url: "eam/equ/plannedMaintenance/index",
    name: "planCount",
    des: "计划修",
  },
];
const getEamInfo = async () => {
  await http({
    method: "POST",
    url: "/eam/equAssetInfo/repairCountList",
    data: {},
  }).then((res) => {
    for (let i = 0; i < menu.value.length; i++) {
      for (let j = 0; j < infoList1.length; j++) {
        if (infoList1[j].url === menu.value[i].url) {
          menu.value[i].info = res.data[infoList1[j].name] ?? 0;
          break;
        }
      }
    }
  });
};

// 首页跳转
function jump(item) {
  console.log("item", item);
  if (item.url === 'eam/equ/pointInspection/index') {
	  // 点检先选设备位置
	  uni.navigateTo({
	  	url:  `/pages/eam/equ/selectPosition/index`,
	  	fail: () => {
	  		uni.showToast({
	  			title: "敬请期待",
	  			icon: "error",
	  		});
	  	},
	  });
	  return
  }
  
  if (item.enabled === 0) {
    uni.showToast({
      title: "菜单暂未启用",
      icon: "error",
    });
  } else {
    uni.navigateTo({
      url: `/pages/${item.url}`,
      fail: () => {
        uni.showToast({
          title: "找不到页面",
          icon: "error",
        });
      },
    });
  }
}
// 点击扫码
const scanCode = ref("");
function openScan() {
  /* #ifdef H5 */
  uni.showToast({
    icon: "none",
    title: "请到安卓设备进行扫码",
  });
  /* #endif */
  // 只允许通过相机扫码
  uni.scanCode({
    scanType: ["barCode"],
    onlyFromCamera: true,
    success: function (res) {
      scanCode.value = JSON.stringify(res);
      console.log("条码类型：" + res.scanType);
      console.log("条码内容：" + res.result);
    },
  });
}

//停机呼叫（安东）
function andon() {
  uni.navigateTo({
    url: "/pages/andon/index",
  });
}
</script>

<style lang="scss" scoped>
.content {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  position: relative;
  margin: 60px 0 20px;
}

.home-navbar {
  &::v-deep .u-navbar__content__left__text {
    font-size: 16px;
  }
  .u-nav-slot {
    width: calc(100% - 220rpx);
  }
  .u-search {
    padding-left: 30rpx;
    &::v-deep .u-search__content {
      height: 35px;
      padding-left: 30rpx;
    }
  }
}
.logo {
  height: 200rpx;
  width: 200rpx;
  margin-top: 200rpx;
  margin-left: auto;
  margin-right: auto;
  margin-bottom: 50rpx;
}

.menu-area {
  /*设置为grid网格布局*/
  width: 100%;
  height: 100%;
  display: grid;
  grid-template-rows: 220rpx 220rpx 220rpx;
  grid-template-columns: 200rpx 200rpx 200rpx;
  grid-gap: 30rpx;
  justify-content: center;
  .menuCard {
    padding-top: 40rpx;
    background-color: #fff;
    border-radius: 16rpx;
    text-align: center;
    border: 1rpx solid rgba(0, 0, 0, 0.06);
    position: relative;
    .info {
      position: absolute;
      right: 5px;
      top: 5px;
    }
    .title {
      margin-top: 20rpx;
      margin-bottom: 30rpx;
      font-size: 28rpx;
    }
  }
}

.title {
  font-size: 36rpx;
  color: #8f8f94;
}
</style>
